package registerMvc;

import registerMvc.UserBean;

import java.util.HashMap;

public class LoginDBUtil {
    private static LoginDBUtil instance =  new LoginDBUtil();
    private HashMap<String, UserBean> users = new HashMap<String,UserBean>();
    private LoginDBUtil(){
        UserBean user1 = new UserBean();
        user1.setName("jack");
        user1.setPassword("123456");
        user1.setEmail("jack@qq.com");
        users.put("jack",user1);

        UserBean user2 = new UserBean();
        user2.setEmail("rose");
        user2.setPassword("123456");
        user2.setEmail("rose@qq.com");
        users.put("rose",user2);
    }


    public static LoginDBUtil getInstance() {return instance;}
    public UserBean getUser(String userName){
        UserBean user  = (UserBean) users.get(userName);
        return user;
    }


//    校验数据
    public boolean checkSata (UserBean user){
        if (user == null){
            return false;
        }
        String userName = user.getName();
        String password = user.getPassword();
        UserBean userBean = getUser(userName);
        if (userBean == null){
            return false;
        }
        if (userBean.getPassword().equals(password)){
            return true;
        }
        return false;
    }

    public boolean inserUser(UserBean user){
        if(user == null){
            return false;
        }
        String userName = user.getName();
        if (users.get(userName)!=null){
            return false;
        }
        users.put(userName,user);
        return true;
    }

}
